<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h2>创建新账户</h2>
        <p>加入我们，享受宠物乐园的所有功能</p>
      </div>
      
      <form class="register-form" @submit.prevent="handleRegister">
        <!-- 用户名 -->
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            id="username"
            type="text"
            v-model="formData.username"
            placeholder="请输入用户名"
            required
          />
          <div v-if="errors.username" class="error-message">{{ errors.username }}</div>
        </div>
        
        <!-- 邮箱 -->
        <div class="form-group">
          <label for="email">邮箱</label>
          <input
            id="email"
            type="email"
            v-model="formData.email"
            placeholder="请输入邮箱"
            required
          />
          <div v-if="errors.email" class="error-message">{{ errors.email }}</div>
        </div>
        
        <!-- 密码 -->
        <div class="form-group">
          <label for="password">密码</label>
          <input
            id="password"
            type="password"
            v-model="formData.password"
            placeholder="请设置密码"
            required
          />
          <div v-if="errors.password" class="error-message">{{ errors.password }}</div>
        </div>
        
        <!-- 确认密码 -->
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            id="confirmPassword"
            type="password"
            v-model="formData.confirmPassword"
            placeholder="请再次输入密码"
            required
          />
          <div v-if="errors.confirmPassword" class="error-message">{{ errors.confirmPassword }}</div>
        </div>
        
        <!-- 提交按钮 -->
        <button type="submit" class="register-button" :disabled="isLoading">
          <span v-if="isLoading">注册中...</span>
          <span v-else>立即注册</span>
        </button>
        
        <!-- 已有账号提示 -->
        <div class="login-link">
          <span>已有账号？</span>
          <router-link to="/login">去登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuth } from '../composables/useAuth'

const router = useRouter()
const { register } = useAuth()

// 表单数据
const formData = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: ''
})

// 错误信息
const errors = ref({})

// 加载状态
const isLoading = ref(false)

// 表单验证
const validateForm = () => {
  let isValid = true
  errors.value = {}
  
  // 验证用户名
  if (!formData.value.username.trim()) {
    errors.value.username = '用户名不能为空'
    isValid = false
  } else if (formData.value.username.length < 3) {
    errors.value.username = '用户名至少需要3个字符'
    isValid = false
  }
  
  // 验证邮箱
  const emailRegex = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/
  if (!formData.value.email.trim()) {
    errors.value.email = '邮箱不能为空'
    isValid = false
  } else if (!emailRegex.test(formData.value.email)) {
    errors.value.email = '请输入有效的邮箱地址'
    isValid = false
  }
  
  // 验证密码
  if (!formData.value.password) {
    errors.value.password = '密码不能为空'
    isValid = false
  } else if (formData.value.password.length < 6) {
    errors.value.password = '密码至少需要6个字符'
    isValid = false
  }
  
  // 验证确认密码
  if (formData.value.password !== formData.value.confirmPassword) {
    errors.value.confirmPassword = '两次输入的密码不一致'
    isValid = false
  }
  
  return isValid
}

// 注册处理
const handleRegister = async () => {
  // 验证表单
  if (!validateForm()) {
    return
  }
  
  isLoading.value = true
  
  try {
    // 使用 useAuth 组合式函数进行注册
    await register({
      username: formData.value.username,
      email: formData.value.email,
      password: formData.value.password
    })
    
    // 注册成功后跳转到个人主页
    router.push('/profile')
  } catch (error) {
    console.error('注册失败:', error)
    alert('注册失败，请稍后再试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
/* 全局样式变量 */
:root {
  --primary-color: #4CAF50;
  --primary-hover: #3d8b40;
  --text-primary: #333;
  --text-secondary: #666;
  --white: #ffffff;
  --light-gray: #f5f5f5;
  --border-color: #e0e0e0;
  --error-color: #f44336;
  --radius: 12px;
  --transition: all 0.3s ease;
  --shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

/* 注册页面容器 */
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background-color: var(--light-gray);
}

/* 注册卡片 */
.register-card {
  background-color: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 500px;
  overflow: hidden;
}

/* 注册头部 */
.register-header {
  background-color: var(--primary-color);
  color: var(--white);
  padding: 30px 30px 20px;
  text-align: center;
}

.register-header h2 {
  font-size: 1.8rem;
  margin-bottom: 8px;
}

.register-header p {
  font-size: 1rem;
  opacity: 0.9;
}

/* 注册表单 */
.register-form {
  padding: 30px;
}

/* 表单组 */
.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: var(--text-primary);
}

.form-group input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid var(--border-color);
  border-radius: var(--radius);
  font-size: 1rem;
  transition: var(--transition);
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary-color);
}

/* 错误信息 */
.error-message {
  color: var(--error-color);
  font-size: 0.875rem;
  margin-top: 4px;
}

/* 注册按钮 */
.register-button {
  width: 100%;
  padding: 14px;
  background-color: var(--primary-color);
  color: var(--white);
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

.register-button:hover:not(:disabled) {
  background-color: var(--primary-hover);
  transform: translateY(-2px);
}

.register-button:disabled {
  background-color: var(--border-color);
  cursor: not-allowed;
}

/* 登录链接 */
.login-link {
  text-align: center;
  margin-top: 20px;
  color: var(--text-secondary);
}

.login-link a {
  color: var(--primary-color);
  text-decoration: none;
  font-weight: 600;
  transition: var(--transition);
}

.login-link a:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-card {
    margin: 20px 0;
  }
  
  .register-header {
    padding: 20px 20px 15px;
  }
  
  .register-header h2 {
    font-size: 1.5rem;
  }
  
  .register-form {
    padding: 20px;
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 10px;
  }
  
  .form-group input {
    padding: 10px 12px;
  }
  
  .register-button {
    padding: 12px;
  }
}
</style>